package com.whfc.emp.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.AppExamAnswerRecordMapper;
import com.whfc.emp.dao.AppExamPaperMapper;
import com.whfc.emp.dao.AppExamPaperQuestionMapper;
import com.whfc.emp.dao.AppTrainingMapper;
import com.whfc.emp.dto.*;
import com.whfc.emp.entity.AppExamPaperQuestion;
import com.whfc.emp.entity.AppTraining;
import com.whfc.emp.param.*;
import com.whfc.emp.service.AppExamPaperService;
import com.whfc.emp.service.AppTrainingCategoriesService;
import com.whfc.emp.service.AppTrainingQuestionsService;
import com.whfc.emp.service.AppTrainingService;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysDictService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClasssName AppTrainServiceImpl
 * @Description 人员管理-培训
 * @Author hw
 * @Date 2020/11/26 16:18
 * @Version 1.0
 */
@DubboService(interfaceClass = AppExamPaperService.class, version = "1.0.0", timeout = 5000)
public class AppExamPaperServiceImpl implements AppExamPaperService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppExamPaperMapper appExamPaperMapper;
    @Autowired
    private AppExamPaperQuestionMapper appExamPaperQuestionMapper;
    @Autowired
    private AppExamAnswerRecordMapper appExamAnswerRecordMapper;
    @Autowired
    private AppTrainingQuestionsService appTrainingQuestionsService;


    @Override
    public PageData<AppExamPaperDTO> list(AppExamPaperListParam request) throws BizException {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AppExamPaperDTO> list = appExamPaperMapper.selectByParam(request);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void add(AppExamPaperAddParam request) throws BizException {

    }

    @Override
    public void edit(AppExamPaperEditParam request) throws BizException {

    }

    @Override
    public void del(Integer id) throws BizException {

    }

    @Override
    public PageData<AppExamPaperDTO> getExamPaperList(AppExamPaperListParam param) {
        return null;
    }

    @Override
    public PageData<AppExamPaperQuestionDTO> examQuestionList(AppExamPaperQuestionListParam reqParam) {
        PageHelper.startPage(reqParam.getPageNum(), reqParam.getPageSize());
        List<AppExamPaperQuestionDTO> list = appExamPaperQuestionMapper.selectByParam(reqParam);
        convertDtoList(list);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }


    private void convertDtoList(List<AppExamPaperQuestionDTO> list) {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        List<Integer> questionIds = list.stream().map(AppExamPaperQuestionDTO::getQuestionId).collect(Collectors.toList());
        Map<Integer, AppTrainingQuestionsDTO> questionMap = appTrainingQuestionsService.getQuestionMap(questionIds);
        list.forEach(item -> {
            item.setQuestion(questionMap.get(item.getQuestionId()));
        });
    }
}
